package cn.micro.keep.accounts.controller;


import cn.micro.keep.accounts.api.Result;
import cn.micro.keep.accounts.common.utils.SecurityUtil;
import cn.micro.keep.accounts.entity.dto.EditPasswordDTO;
import cn.micro.keep.accounts.entity.dto.UserDTO;
import cn.micro.keep.accounts.service.AccountDimUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/accounts/accountDimUser")
@RequiredArgsConstructor
@Validated
@Slf4j
public class AccountDimUserController {
    private final AccountDimUserService accountDimUserService;


    @GetMapping("info")
    public Object info(){
        return new Result<>().success(accountDimUserService.getDetail(SecurityUtil.getUserId()));
    }

    @GetMapping("commonStat")
    public Object commonStat(){
        return new Result<>().success(accountDimUserService.commonStat());
    }

    @PutMapping("editPassword")
    public Object editPassword(@Validated @RequestBody EditPasswordDTO dto){
        if (!accountDimUserService.editPassword(dto)){
            return new Result<>().fail("修改失败");
        }
        return new Result<>().success("修改成功");
    }

    @PutMapping("editInfo")
    public Object editInfo(@RequestBody UserDTO dto){
        if (!accountDimUserService.editUserInfo(dto)){
            return new Result<>().fail("修改失败");
        }
        return new Result<>().success();
    }

    @DeleteMapping("delete")
    public Object delete(){
        if (!accountDimUserService.removeById(SecurityUtil.getUserId())){
            return new Result<>().fail("注销失败");
        }
        return new Result<>().success("注销成功");
    }
}
